#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int N=1e6+10;
char s[N];
int a[6][N];
unordered_map <char,int> mp;
int main()
{
	int t;
	cin>>t;
	mp['A']=1;
	mp['B']=10;
	mp['C']=100;
	mp['D']=1000;
	mp['E']=10000;
	while(t--)
	{
		int n;
		scanf("%s",s+1);
	    int len=strlen(s+1);
	    for(int i=1;i<=len;i++)
	    {
	    	for(int j=1;j<=5;j++)
	    	{
	    		a[j][i]=a[j][i-1];
			}
	    	a[s[i]-'A'+1][i]++;
		}
		ll ans=mp[s[len]];
		char mc=s[len];
		vector<int> v;
		v.push_back(len);
		for(int i=len-1;i>0;i--)
		{
			if(s[i]<mc)
			{
				ans-=mp[s[i]];
			 }
			 else
			 {
			 	ans+=mp[s[i]];
			 	mc=s[i];
			 	v.push_back(i);
			 }
		}
		cout<<ans<<endl;
		ll sum=ans-mp[s[1]]+10000;
		sort(v.begin(),v.end());
		for(int i=1;i<v.size();i++)
		{
			ll sum1=0;
			sum=max(ans-mp[s[v[i]]]+mp[s[v[i-1]]],sum);
			
                    if(i+1!=v.size())
					for(int j=s[v[i]]-'A';j>=s[v[i+1]]-'A'+1;j--)
					{
					    sum1=ans-mp[s[v[i]]]+mp[j+'A'-1];
						for(int k=j;k<s[v[i]]-'A'+1;k++)
						{
							sum1=sum1+2*(a[k][v[i]-1]-a[k][v[i-1]])*mp[k+'A'-1];
						}
					}
					else
					{
						for(int j=s[v[i]]-'A';j>=0;j--)
					{
						ll sum1=ans-mp[s[v[i]]]+mp[j+'A'-1];
						for(int k=j;k<s[v[i-1]]-'A'+1;k++)
						{
							sum1=sum1+2*(a[k][v[i]-1]-a[k][v[i-1]])*mp[k+'A'-1];
						}
					}
					}
					sum=max(sum,sum1);

	
 		}
 		ans=max(ans,sum);
 		cout<<ans<<endl;
	}

	return 0;
}

